<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{User as Users, HallTrade, Config};
use think\Db;
use app\facade\Redis;

class Hall extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Hall')->setDescription('订单超时自动完成或取消');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/rskj/think Hall'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        $matchtime = date('Y-m-d H:i:s', time() - 3600);
        (new HallTrade())->where(['status' => [2, 3]])->where('matchtime|paycreated', '<', $matchtime)->chunk(100, function ($list) use ($matchtime) {
            foreach ($list as $lists) {
                //未付款订单取消
                if ($lists['status'] == 2 && $lists['matchtime'] < $matchtime) {
                    $user = (new Users())->where('id', $lists['seller_id'])->find(); //卖家信息
                    Db::startTrans();
                    try {
                        //订单直接取消
                        (new HallTrade())->where(['id' => $lists['id']])->update(['status' => 5]);
                        (new Users())->MoneyEdit($lists['seller_id'], $user['amount'], $lists['num'], 2, 3, '自动取消订单', $lists['id'], 0, 0, 0);
                        //手续费操作
                        if ($lists['handfee'] > 0) {
                            (new Users())->MoneyEdit($lists['seller_id'], ($user['amount'] + $lists['num']), $lists['handfee'], 2, 4, '自动取消挂单手续费', $lists['id'], 0, 0, 0);
                        }
                        //额度操作
                        (new Users())->MoneyEdit($lists['seller_id'], $user['quota'], $lists['num'], 3, 2, '售卖挂单自动取消', $lists['id'], 0, 0, 0);
                        //信誉值操作
                        (new Users())->where('id', $lists['buyer_id'])->setDec('reputa', 1);
                        Db::commit();
                    } catch (\Exception $e) {
                        dump($e->getMessage());
                        Db::rollback();
                    }
                    echo '取消成功';
                }
                //未确认订单完结
                if ($lists['status'] == 3 && $lists['paycreated'] < $matchtime) {
                    $quota = 0;
                    $quotaratio = (new Config())->where('key', 'hallquote')->value('value');
                    $quotaratio =  $quotaratio ?? 1.5;
                    $quota = round($lists['num'] * $quotaratio, 2);
                    $user = (new Users())->where('id', $lists['buyer_id'])->find(); //买家信息
                    Db::startTrans();
                    try {
                        (new HallTrade())->where(['id' => $lists['id']])->update(['status' => 4]);
                        //买家币和信誉
                        if ($lists['num'] > 0) {
                            (new Users())->MoneyEdit($user['id'], $user['money'], $lists['num'], 1, 2, 'OTC自动购买', $lists['id']);
                            //额度操作
                            (new Users())->MoneyEdit($user['id'], $user['quota'], $quota, 3, 1, 'OTC自动购买', $lists['id']);
                        }
                        //信誉值操作
                        (new Users())->where('id', $lists['seller_id'])->setDec('reputa', 1);
                        Db::commit();
                    } catch (\Exception $e) {
                        dump($e->getMessage());
                        Db::rollback();
                    }
                    echo '完结成功';
                }
            }
        });
    }
}
